package org.whp.mealplanner.server;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddIngredientServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String ingredientName = req.getParameter("name");
		String ingredientUnits = req.getParameter("units");
		
		Ingredient newIngredient = new Ingredient(ingredientName, ingredientUnits);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(newIngredient);
		} finally {
			pm.close();
		}
		resp.sendRedirect("addMeal.jsp?ingadd=1");
		
	}
}
